import _Table from "choerodon-ui/pro/lib/table";
import _Button from "choerodon-ui/pro/lib/button";
import _Select from "choerodon-ui/pro/lib/select";
import _DataSet from "choerodon-ui/pro/lib/data-set";
import _map from "lodash/map";
import React, { useContext } from 'react';
import { observer } from 'mobx-react-lite';
import Store from "./stores";
import { getEditorByField } from "../utils";
import { OPERATOR_TYPE, OPERATOR } from "../constant";
var ConditionTable = observer(function () {
  var _useContext = useContext(Store),
      conditionDataSet = _useContext.conditionDataSet,
      tableDataSet = _useContext.tableDataSet,
      fieldArr = _useContext.fieldArr;

  function handleFieldChange() {
    var value = conditionDataSet.current.get('fieldName');
    var comparator = conditionDataSet.current.getField('comparator');

    if (comparator.get('options') && comparator.get('options').toData().length === OPERATOR_TYPE.ALL.length) {
      _map(fieldArr, function (field) {
        if (value === field.value) {
          comparator.set('options', new _DataSet({
            selection: 'single',
            data: OPERATOR_TYPE[field.type.toUpperCase()]
          }));
        }
      });
    }
  }

  var columns = [{
    name: 'fieldName',
    editor: /*#__PURE__*/React.createElement(_Select, {
      searchable: true
    })
  }, {
    name: 'comparator',
    editor: /*#__PURE__*/React.createElement(_Select, {
      onFocus: function onFocus() {
        return handleFieldChange();
      }
    })
  }, {
    name: 'value',
    editor: function editor(record) {
      if (record.get('fieldName') && record.get('comparator') && ![OPERATOR.IS_NULL.value, OPERATOR.IS_NOT_NULL.value].includes(record.get('comparator'))) {
        var tableField = tableDataSet.getField(record.get('fieldName'));
        return tableField ? getEditorByField(tableField) : false;
      }

      return false;
    }
  }];
  var addBtn = /*#__PURE__*/React.createElement(_Button, {
    funcType: "flat",
    icon: "add",
    key: "condition-add-btn",
    onClick: function onClick() {
      return conditionDataSet.create({}, 0);
    }
  }, "\u65B0\u589E");
  return /*#__PURE__*/React.createElement(_Table, {
    header: "\u6761\u4EF6",
    buttons: [addBtn, 'delete'],
    dataSet: conditionDataSet,
    columns: columns
  });
});
export default ConditionTable;